$(document).ready(function ($) {
    var url = window.location.href,
        indexReg = /index/,
        limit = 10,
        offset = 0;

    console.log("webExtensionWallet-->" + window.webExtensionWallet)

    //tx = "a5c6ca62603535e14383cc2b310d5dd96e142bd1f2edc88fe1eaf77b70117eaa"
    var dappAddress = "n1iLrEqtwuWoDmSbyDqKbqDrVPypRrjMpRK";
    var voteFn = {

        setStorage: function (key, obj) {
            localStorage.setItem(key, JSON.stringify(obj));
        },

        getStorage: function (key) {
            return JSON.parse(localStorage.getItem(key));
        },

        delteStorage: function (key) {
            localStorage.removeItem(key);
        },

        /**
         * 拼接投票信息字符串
         */
        userStr: function (objs) {
            var str = '';
            for (var i = 0; i < objs.length; i++) {
                str += '<li>'
                    + '<div class="head">'
                    + '<img src="' + objs[i].head_icon + '" alt="">'
                    + '</a>'
                    + '</div>'
                    + '<div class="up">'
                    + '<div class="vote">'
                    + '<span class="voteCnt" id=' + objs[i].username + "_voteCnt" + '>' + objs[i].vote + '票</span>'
                    + '</div>'
                    + '<div class="btn" id=' + objs[i].username + '>'
                    + '投TA一票'
                    + '</div>'
                    + '</div>'
                    + '<div class="descr">'
                    + '<div>'
                    + '<span>' + objs[i].username + '</span>'
                    + '<span>|</span>'
                    + '<span>编号#' + objs[i].id + '</span>'
                    + '</div>'
                    + '<a href=' + objs[i].url + '>'
                    + '<p>' + objs[i].descrption + '</p>'
                    + '</a>'
                    + '</div>'
                    + '</li>';
            }
            return str;
        },

        /**
         * 投票事件绑定
         */
        userPoll: function () {
            $('.btn').off();
            $('.btn').click(function (event) {
                console.log("userPoll-->")
                var id = $(this).attr('id');
                var callFunction = "save";
                var callArgs = "[\"" + id + "\"]"; //in the form of ["args"]
                console.log("ID-->" + id)
                console.log(window.webExtensionWallet)
                nebPay.simulateCall(dappAddress, 0, callFunction, callArgs, {
                    listener: cbCallDapp
                });

                function cbCallDapp(resp) {
                    // console.log("cbCallDapp-->")
                    // console.log(resp)
                    // console.log(JSON.parse(resp.result))
                    var result = JSON.parse(resp.result);
                    if (result) {
                        if (result.status <= 0) {
                            alert(result.msg);
                        } else {
                            nebPay.call(dappAddress, 0, callFunction, callArgs, {
                                listener: callsave
                            });
                        }
                    }
                }

                function callsave(resp) {
                    console.log(resp)
                    alert("投票成功！！投票结果可能根据区块情况有所延迟！")
                }

            });

        },

        /**
         * 投票数更新
         * @param objs
         */
        voteCnt: function (objs) {
            for (var i = 0; i < objs.length; i++) {
                var callFunction = "take";
                console.log(objs[i].username)
                var callArgs = "[\"" + objs[i].username + "\"]"; //in the form of ["args"]
                nebPay.simulateCall(dappAddress, 0, callFunction, callArgs, {
                    listener: function (resp) {
                        var key = '#' + JSON.parse(resp.result).voteType + "_voteCnt"
                        $(key).html(JSON.parse(resp.result).voteCnt + '票')
                    }
                });
            }

        },

    };
    if (indexReg.test(url)) {

        $.ajax({
            url: '/vote/index/data?limit=10&offset=0',
            type: 'GET',
            success: function (data) {
                offset += limit;
                data = JSON.parse(data);
                $('.coming').append(voteFn.userStr(data.data.objects));
                voteFn.userPoll();
                voteFn.voteCnt(data.data.objects);
                //定时更新投票数据
                intervalQuery = setInterval(function () {
                    console.log("----->intervalQuery")
                    voteFn.voteCnt(data.data.objects);
                }, 10000);
            }
        });

    }
});